/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
#include"PublicTool.h"
#include"PublicLog.h"
#include"PublicCry.h"
#include"CSwitchManager.h"
#include"PublicRRDTool.h"
#include"SnmpApi.h"
void toolhelp(){
	INFO("-dec XXX 解密");
	INFO("-enc XXX 加密");
	INFO("-update -ip:X.X.X.X -community:XXX 更新指定设备数据");
//	INFO("-recount -ip:X.X.X.X -s:starttime -e:endtime 重新计算指定IP带宽");
//	INFO("若IP为all 则重聚所有IP带宽");
//	INFO("时间格式可为时间戳或日期 如 1483200000,20170101,2017010100000000");
#if USE_PUBLIC_TOOL  	
	INFO("-rrddump  -ip:X.X.X.X -s:starttime -e:endtime -if:ifname -rrd:xxx.rrd 将网卡带宽导出到指定文件");
	INFO("-rrdgraph -s:starttime -e:endtime -rrd:xxx.rrd -png:xxx.png 将rrd中带宽画出");
#endif	
}
void recount(int argc, char** argv);
void updatedata(int argc, char** argv);
#if USE_PUBLIC_TOOL  
void rrddump(int argc, char** argv);
void rrdgraph(int argc, char** argv);
#endif
void tool(int argc, char** argv){
	int i=1;
	for(i=1;i<argc;i++){
		if(strcmp(argv[i],"-dec")==0){
			if((i+1)==argc){
				WARN("请输入要解密字符串");
			}else{
				char value[10240];
				strcpy(value, argv[i+1]);
				if((!DECRYPT((unsigned char*)value, strlen(value)))){
					WRONG("解密失败");
				}else{
					INFO("解密结果:%s", value);
				}
			}
			break;
		}else
		if(strcmp(argv[i],"-enc")==0){
			if((i+1)==argc){
				WARN("请输入要加密字符串");
			}else{
				char value[10240];
				strcpy(value, argv[i+1]);
				if((!ENCRYPT((unsigned char*)value, strlen(value)))){
					WRONG("加密失败");
				}else{
					INFO("加密结果:%s", value);
				}
			}
			break;
		}else
		if(strcmp(argv[i],"-update")==0){
			updatedata(argc-i,argv+i);
			break;
		}else	
		if(strcmp(argv[i],"-recount")==0){
			recount(argc-i,argv+i);
			break;
#if USE_PUBLIC_TOOL  			
		}else
		if(strcmp(argv[i],"-rrddump")==0){
			rrddump(argc-i,argv+i);
			break;
		}else
		if(strcmp(argv[i],"-rrdgraph")==0){
			rrdgraph(argc-i,argv+i);
			break;
#endif			
		}else{
			toolhelp();
			break;
		}
	}
}
void updatedata(int argc, char** argv){
	int i=1;
	char IP[64]={0};
	char Community[256]={0};
	for(i=1;i<argc;i++){
		if(strncmp(argv[i],"-ip:",4)==0){
			strcpy(IP,argv[i]+4);
		}else
		if(strncmp(argv[i],"-community:",11)==0){
			strcpy(Community,argv[i]+11);
		}
	}
	if(IsIPV4(IP)==0){
		WARN("IP错误");
		return;
	}
	if(strlen(Community)<1){
		WARN("团体号错误");
		return;
	}
	UpdateSnmpCommunity(IP,Community);
	SNMPSYSPACKET info=GetSnmpInfoByIP(IP,Community,3000);
	if(info.snmptime>0){
		CSwitchData*tmp=new CSwitchData(IP);	
		tmp->UpdateSwitchSysInfo(info);
		tmp->UpDateSwitchData();
		tmp->UpLoadToServer();
		delete tmp;
		CSwitchifTable*ifdata=new CSwitchifTable(IP);
		ifdata->UpDateifTable();
		delete ifdata;
	}else{
		WARN("IP:%s SNMP不通，请确认后重试",IP);
	}
}
void recount(int argc, char** argv){
	int i=1;
	char IP[64]={0};
	time_t starttime=0;
	time_t endtime=0;
	for(i=1;i<argc;i++){
		if(strncmp(argv[i],"-ip:",4)==0){
			strcpy(IP,argv[i]+4);
		}else
		if(strncmp(argv[i],"-s:",3)==0){
			starttime=strtotime(argv[i]+3,0);
		}else
		if(strncmp(argv[i],"-e:",3)==0){
			endtime=strtotime(argv[i]+3,1);
		}
	}
	if(strlen(IP)<1||starttime==0||endtime==0){
		WARN("参数不足");
		return;
	}else{
		char timestr[32]={0};
		INFO("IP:%s",IP);		
		INFO("starttime:%s(%ld)",timetostr(starttime,timestr,sizeof(timestr)),starttime);
		INFO("endtime:%s(%ld)",timetostr(endtime,timestr,sizeof(timestr)),endtime);
	}
	if(strcmp(IP,"all")==0){
		CSwitchManager tmp;
		tmp.UpdateSwitchIPLIST();
//		tmp.UpdateSwitchifFlow(starttime,endtime);
	}else
	if(IsIPV4(IP)==1){
		CSwitchifTable tmp(IP);
//		tmp.UpDateifFlowAll(starttime,endtime);
	}else{
		WRONG("IP有误");
	}
}
#if USE_PUBLIC_TOOL  
void rrddump(int argc, char** argv){
	int i=1;
	char IP[32]={0};
	char ifDescr[32]={0};
	char rrdfile[64]={0};
	time_t starttime=0;
	time_t endtime=0;
	for(i=1;i<argc;i++){
		if(strncmp(argv[i],"-ip:",4)==0){
			strcpy(IP,argv[i]+4);
		}else
		if(strncmp(argv[i],"-if:",4)==0){
			strcpy(ifDescr,argv[i]+4);
		}else
		if(strncmp(argv[i],"-rrd:",5)==0){
			strcpy(rrdfile,argv[i]+5);
		}else
		if(strncmp(argv[i],"-s:",3)==0){
			starttime=strtotime(argv[i]+3,0);
		}else
		if(strncmp(argv[i],"-e:",3)==0){
			endtime=strtotime(argv[i]+3,1);
		}
	}
	if(strlen(IP)<1||strlen(ifDescr)<1||strlen(rrdfile)<1||starttime==0||endtime==0){
		WARN("参数不足");
		return;
	}else{
		INFO("IP:%s ifDescr:%s rrdfile:%s",IP,ifDescr,rrdfile);
		INFO("starttime:%s(%ld)",timetostr(starttime),starttime);
		INFO("endtime:%s(%ld)",timetostr(endtime),endtime);
	}
	if(IsIPV4(IP)==1){
		CSwitchifTable tmp(IP);
		tmp.CreateRRDTool(ifDescr,rrdfile,starttime,endtime);
	}else{
		WRONG("IP有误");
	}
}
void rrdgraph(int argc, char** argv){
	int i=1;
	char pngfile[32]={0};
	char rrdfile[64]={0};
	time_t starttime=0;
	time_t endtime=0;
	for(i=1;i<argc;i++){
		if(strncmp(argv[i],"-png:",5)==0){
			strcpy(pngfile,argv[i]+5);
		}else
		if(strncmp(argv[i],"-rrd:",5)==0){
			strcpy(rrdfile,argv[i]+5);
		}else
		if(strncmp(argv[i],"-s:",3)==0){
			starttime=strtotime(argv[i]+3,0);
		}else
		if(strncmp(argv[i],"-e:",3)==0){
			endtime=strtotime(argv[i]+3,1);
		}
	}
	if(strlen(pngfile)<1||strlen(rrdfile)<1||starttime==0||endtime==0){
		WARN("参数不足");
		return;
	}else{
		INFO("rrdfile:%s pngfile:%s",rrdfile,pngfile);
		INFO("starttime:%s(%ld)",timetostr(starttime),starttime);
		INFO("endtime:%s(%ld)",timetostr(endtime),endtime);
	}
	GraphTrafficRRDTool(pngfile,rrdfile,starttime,endtime);
}
#endif
